<?php

namespace app\common\model;

use think\Db;

class ShopMember extends BaseModel
{
    protected function getResidueNumAttr($val,$data){
        $num = $data['total_num'] - $data['use_num'];
        return $num;
    }
    
    protected function getExpireCardAttr($val,$data){
        return date('Y-m-d',$data['expire_time']);
    }

    protected function getResidueBedNumAttr($val,$data){
        $num = $data['bed_num'] - $data['bed_use_num'];
        return $num;
    }

    public function card(){
        return $this->belongsTo(ShopMemberCard::class,'card_id','id');
    }

    //用户除了card_id以外的card
    public function  beyondCardId($uid){

        $haveCardId = Db::name('ShopMember')->where('uid',$uid)->select();
        $CardIdStr = '';
        for ($i=0;$i<count($haveCardId);$i++){
            $CardIdStr .= $haveCardId[$i]['card_id'];
            if ($i < count($haveCardId)-1){
                $CardIdStr .= ',';
            }
        }
        return $CardIdStr;
    }
}
